#include "msgq.h"
#include "bank.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int msgid = 0;
MSG_CTOS ctos = {};
MSG_STOC stoc = {};

void login(void)
{
	char path[80] = {};
	char path0[40] = ACCOUNT_PATH;
	sprintf(path,"%s%s.dat",path0,ctos.acc.id);
	
	if(0 == access(path,F_OK))
	{
		int fd = open(path,O_RDWR);
		int ret1 = read(fd,&stoc.acc,sizeof(Account));
		close(fd);
		if(0 != ret1)
		{
			if(3 > stoc.acc.islock)
			{
				if(0 == strcmp(ctos.acc.pass,stoc.acc.pass))
				{
					strcpy(stoc.info,"success");
				}	
				else
				{
					strcpy(stoc.info,"pass");
					stoc.acc.islock++;
					int fd2 = open(path,O_RDWR);
					int ret2 = write(fd2,&stoc.acc,sizeof(Account));
					close(fd2);
					if(0 == ret2)
					{
						strcpy(stoc.info,"write");
						puts(stoc.info);
					}
				}
			}
			else
			{
				
				strcpy(stoc.info,"lock");
			}
		}
		else
		{
			strcpy(stoc.info,"read");
			puts(stoc.info);	//服务器出错输出错误位置
		}
	}
	else
	{
		strcpy(stoc.info,"false");
	}
}

int main(int argc,const char* argv[])
{
	printf("argv %s\n",argv[0]);
	msgid = get_msg(ACCOUNT_PATH,SUBP_CNT);
	
	for(;;)
	{
		recv_msg(msgid,&ctos,sizeof(MSG_CTOS),MSGTY_LOGIN);
		login();
		stoc.type = ctos.return_type;
		send_msg(msgid,&stoc,sizeof(MSG_STOC));
	}	
}
